package com.my.learn.exercise.thread;
/*
 * 创建人：baimiao
 * 创建时间：2024/10/23 9:50
 *
 */

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ThreeThreadOrderExecute {

    public static void main1(String[] args) throws InterruptedException {

        ReentrantLock lock = new ReentrantLock();
        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();
        Condition condition3 = lock.newCondition();


        Thread t1 = new Thread(() -> {
            for (; ; ) {
                lock.lock();
                try {
                    System.out.println("A");
                    TimeUnit.MILLISECONDS.sleep(200);
                    condition2.signal();
                    condition1.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        });
        Thread t2 = new Thread(() -> {

            for (; ; ) {
                lock.lock();
                try {
                    System.out.println("B");
                    TimeUnit.MILLISECONDS.sleep(200);
                    condition3.signal();
                    condition2.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        });
        Thread t3 = new Thread(() -> {

            for (; ; ) {
                lock.lock();
                try {
                    System.out.println("C");
                    TimeUnit.MILLISECONDS.sleep(200);
                    condition1.signal();
                    condition3.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        });
        t1.start();
        t1.join(500);
        t2.start();
        t2.join(500);
        t3.start();
    }

    public static void main(String[] args) throws InterruptedException {
        String[] arr = {"A", "B", "C", "D"};
        int length = arr.length;
        AtomicInteger index = new AtomicInteger(0);

        ReentrantLock lock = new ReentrantLock();
        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();

        Thread t1 = new Thread(() -> {
            for (; ; ) {
                lock.lock();
                try {
                    int v = index.get();
                    if (v == length) {
                        index.set(0);
                        v = 0;
                    }
                    System.out.println("ThreadA--" + arr[v]);
                    index.incrementAndGet();
                    condition2.signal();
                    condition1.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        });


        Thread t2 = new Thread(() -> {
            for (; ; ) {
                lock.lock();
                try {
                    int v = index.get();
                    if (v == length) {
                        index.set(0);
                        v = 0;
                    }
                    System.out.println("ThreadB--" + arr[v]);
                    index.incrementAndGet();
                    condition1.signal();
                    condition2.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        });

        t1.start();
//        t1.join(100);
        t2.start();

    }
}
